//
//  KeyBoardTypeView.m
//  GuoBang
//
//  Created by 梧桐树 on 16/4/28.
//  Copyright © 2016年 梧桐树. All rights reserved.
//

#import "KeyBoardTypeView.h"

@implementation KeyBoardTypeView
-(instancetype)initWithFrame:(CGRect)frame{
    
    
    self = [super initWithFrame:frame];
    if (self) {
        
        dataArry=@[@{@"icon":@"ico_yuyin@2x.png",@"title":@"语音"},@{@"icon":@"ico_biaoqing@2x.png",@"title":@"表情"},@{@"icon":@"ico_pic@2x.png",@"title":@"图片"},@{@"icon":@"ico_camera@2x.png",@"title":@"拍照"}];
        //创建布局对象
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //        //设置单元格的尺寸
        //        flowLayout.itemSize = CGSizeMake(80, 80);
        //        //设置头视图高度
        //        flowLayout.headerReferenceSize = CGSizeMake(0, 30);
        //flowlaout的属性，横向滑动
        //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        col = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60) collectionViewLayout:flowLayout];
        col.delegate=self;
        col.dataSource=self;
        [self addSubview:col];
        [col registerClass:[KeyBoardCell class] forCellWithReuseIdentifier:@"KeyBoardCell"];
       
        self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, WIDTH, col.frame.size.height);
        self.backgroundColor=[UIColor whiteColor];
        col.backgroundColor=[UIColor whiteColor];
    }
    return self;
}

#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataArry.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    static NSString * CellIdentifier = @"FunctionCell";
    //    FunctionCell * cell = (FunctionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    //
    UINib *nib = [UINib nibWithNibName:@"KeyBoardCell"
                                bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"KeyBoardCell"];
    KeyBoardCell *cell = [[KeyBoardCell alloc]init];
    
    // Set up the reuse identifier
    cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"KeyBoardCell"
                                                     forIndexPath:indexPath];
    cell.title.text=[[dataArry objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.icon.image=[UIImage imageNamed:[[dataArry objectAtIndex:indexPath.row] objectForKey:@"icon"]];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(WIDTH/4, 60);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate touchKey:(int)indexPath.row];
//    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
       //cell.backgroundColor = [UIColor whiteColor];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



@end
